﻿using CommunityToolkit.Mvvm.ComponentModel;
using RotovapPlatform.UI.Navigation;
using System.Windows.Controls;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics;
using RotovapPlatform.UI.ViewModels.RecipeManagement;
using RotovapPlatform.UI.Views.MaterialManagement;
using RotovapPlatform.UI.Views;
using Microsoft.Extensions.Logging;
using RotovapPlatform.UI.ViewModels.ExperimentManagement;
using RotovapPlatform.UI.Views.ExperimentManagement;
using RotovapPlatform.UI.Views.RecipeManagement;
using RotovapPlatform.UI.Views.DeviceMaintenance;
using RotovapPlatform.UI.Views.DataRecord;

namespace RotovapPlatform.UI.ViewModels.Navigation;

public partial class NavigationViewModel : ObservableObject
{
	private readonly IServiceProvider _serviceProvider;
	private readonly Dictionary<NavigationItem, UserControl> _viewCache = new();
	private readonly ILogger<NavigationViewModel> _logger;

	[ObservableProperty]
	private UserControl _currentView;

	public NavigationViewModel(IServiceProvider serviceProvider, ILogger<NavigationViewModel> logger)
	{
		_serviceProvider = serviceProvider;
		_logger = logger;
	}

	public async Task NavigateTo(NavigationItem navigationItem)
	{
		try
		{
			Debug.WriteLine($"Navigating to: {navigationItem}");

			if (!_viewCache.TryGetValue(navigationItem, out var view))
			{
				view = navigationItem switch
				{
					NavigationItem.MainOperation => _serviceProvider.GetRequiredService<MainOperationView>(),
					NavigationItem.ExperimentHistory => _serviceProvider.GetRequiredService<HistoryExperimentView>(),
					NavigationItem.RealTimeData => _serviceProvider.GetRequiredService<RealTimeDataView>(),

					//配方管理	
					NavigationItem.RecipeEditor => _serviceProvider.GetRequiredService<RecipeEditorView>(),
					NavigationItem.ExperimentGroup => _serviceProvider.GetRequiredService<HistoryExperimentGroupView>(),

					//物料管理
					NavigationItem.MaterialDetails => _serviceProvider.GetRequiredService<MaterialQueryView>(),
					NavigationItem.ShelfDetails => _serviceProvider.GetRequiredService<MaterialManagementView>(),
					NavigationItem.StockInOut => _serviceProvider.GetRequiredService<StockInOutView>(),
					NavigationItem.StockInOutRecord => _serviceProvider.GetRequiredService<StockInOutRecordView>(),

					//数据记录
					NavigationItem.DeviceAlarm => _serviceProvider.GetRequiredService<DeviceAlarmRecordView>(),
					NavigationItem.OperationHistory => _serviceProvider.GetRequiredService<OperationLogView>(),
					NavigationItem.HistoricalData => _serviceProvider.GetRequiredService<HistoryProcessDataView>(),


					//设备维护
					NavigationItem.MaintenanceHistory => _serviceProvider.GetRequiredService<DeviceMaintenanceView>(),
					NavigationItem.DeviceParameter => _serviceProvider.GetRequiredService<DeviceParameterView>(),
					NavigationItem.MaintenanceRemind => _serviceProvider.GetRequiredService<MaintenanceReminderView>(),
					NavigationItem.ManualControl => _serviceProvider.GetRequiredService<ManualOperationView>(),

					// 其他导航项...
					_ => throw new ArgumentOutOfRangeException(nameof(navigationItem), $"未实现的导航项: {navigationItem}")
				};

				// 特殊初始化逻辑
				if (view is RecipeEditorView recipeView &&
					recipeView.DataContext is RecipeEditorViewModel vm)
				{
					await vm.InitializeAsync();
				}

				//// 主运行界面初始化逻辑
				//if (view is MainOperationView mainOperationView &&
				//	mainOperationView.DataContext is MainOperationViewModel mainOperationVm)
				//{
				//	await mainOperationVm.InitializeAsync();
				//}

				_viewCache[navigationItem] = view;
			}

			CurrentView = view;
			Debug.WriteLine($"Navigation completed: {view.GetType().Name}");
		}
		catch (Exception ex)
		{
			_logger.LogError(ex, "Navigation failed");
			Debug.WriteLine($"Navigation failed: {ex.Message}");
		}
	}
}